//
//  UITextView+JKInputLimit.m
//  JKCategories (https://github.com/shaojiankui/JKCategories)
//
//  Created by Jakey on 2016/11/29.
//  Copyright © 2016年 www.skyfox.org. All rights reserved.
//

#import "UITextView+NEDInputLimit.h"
#import <objc/runtime.h>
#import "NSString+NED.h"
#import "NSNotificationCenter+NED.h"
#import <NEDConstant/NEDDefines.h>
#import "NEDConstant.h"
#import "NEDApp+Mediator.h"

@implementation UITextView (NEDInputLimit)


-(void (^)(void))changeBlock{
    return objc_getAssociatedObject(self, @selector(changeBlock));
}
-(void)setChangeBlock:(void (^)(void))changeBlock{
    objc_setAssociatedObject(self, @selector(changeBlock), changeBlock, OBJC_ASSOCIATION_COPY);
}

- (NSInteger)ned_maxLengthBytes {
    return [objc_getAssociatedObject(self, @selector(ned_maxLengthBytes)) integerValue];
}
- (void)setNed_maxLengthBytes:(NSInteger)ned_maxLengthBytes {
    objc_setAssociatedObject(self, @selector(ned_maxLengthBytes), @(ned_maxLengthBytes), OBJC_ASSOCIATION_ASSIGN);
    WS(weakSelf)
    [NSNotificationCenter ned_addObserver:self name:UITextViewTextDidChangeNotification usingBlock:^(NSNotification * _Nonnull note, id  _Nonnull observer) {
        [weakSelf ned_bytesTextViewTextDidChange:note];
    }];
}


- (void)ned_bytesTextViewTextDidChange:(NSNotification *)notification {
    NSString *toBeString = self.text;
    NSString *lang = [[self textInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [self markedTextRange];
        //获取高亮部分(感觉输入中文的时候才有)
        UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position){
            //中文和字符一起检测  中文是两个字符
            if ([toBeString ned_stringLenthOfBytes] > self.ned_maxLengthBytes){
                self.text = [toBeString ned_subBytesOfstringToIndex:self.ned_maxLengthBytes];
            }
        }
    }else{
        if ([toBeString ned_stringLenthOfBytes] > self.ned_maxLengthBytes){
            self.text = [toBeString ned_subBytesOfstringToIndex:self.ned_maxLengthBytes];
        }
    }
    
    if(self.changeBlock){
        self.changeBlock();
    }
}





- (NSInteger)ned_maxLength {
    return [objc_getAssociatedObject(self, @selector(ned_maxLength)) integerValue];
}
- (void)setNed_maxLength:(NSInteger)ned_maxLength {
    objc_setAssociatedObject(self, @selector(ned_maxLength), @(ned_maxLength), OBJC_ASSOCIATION_ASSIGN);
    WS(weakSelf)
    [NSNotificationCenter ned_addObserver:self name:UITextViewTextDidChangeNotification usingBlock:^(NSNotification * _Nonnull note, id  _Nonnull observer) {
        [weakSelf ned_textViewTextDidChange:note];
    }];
}

- (void)ned_textViewTextDidChange:(NSNotification *)notification {
    
    NSString *toBeString = self.text;
    if (toBeString.length > self.ned_maxLength){
        [APP.mediator showMessage:[NSString stringWithFormat:@"最多%ld个字",self.ned_maxLength]];
        self.text = [toBeString substringToIndex:self.ned_maxLength];
    }
    
    if(self.changeBlock){
        self.changeBlock();
    }
}

@end
